package com.wangsd.web.face.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangsd.web.face.entity.Device;
import com.wangsd.web.face.mapper.DeviceMapper;
import com.wangsd.web.face.service.DeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author generator
 * @since 2019-09-09
 */
@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService {

    @Resource
    DeviceMapper deviceMapper;

    @Override
    public IPage<Device> selectList(Integer pageNum, Integer pageSize, Device device) {
        Page<Device> page = new Page<>(pageNum, pageSize);
        IPage<Device> deviceIPage = deviceMapper.selectListXml(page, device);
        return deviceIPage;
    }

    @Override
    @Transactional
    public void addOrUpdate(Device device) {
        if (device.getId() != null) {
            deviceMapper.updateById(device);
        } else {
            deviceMapper.insert(device);
        }
    }

    @Override
    public Device selectByDeviceNo(String deviceNo) {
        return deviceMapper.selectOne(new QueryWrapper<Device>().eq("device_no", deviceNo).eq("deleted", 0));
    }

    @Override
    public List<Device> selectAllByUserCode(String code) {
        return deviceMapper.selectAllByUserCodeXml(code);
    }

    @Override
    public List<Device> selectByStoreId(Long id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("store_id", id);
        queryWrapper.eq("deleted", "0");
        return deviceMapper.selectList(queryWrapper);
    }

    @Override
    public void deleteDeviceStoreId(Long deviceId) {
        deviceMapper.deleteDeviceStoreIdXml(deviceId);
    }

    @Override
    public void addDeviceStoreId(Long storeId, Long deviceId) {
        Device device = deviceMapper.selectById(deviceId);
        device.setStoreId(storeId);
        deviceMapper.updateById(device);
    }

    @Override
    public void removeDeviceStoreIdByStoreId(Long id) {
        deviceMapper.removeDeviceStoreIdByStoreId(id);
    }
}
